#include "simpleiter.h"

simpleiter::simpleiter(QWidget *parent, Qt::WFlags flags)
    : QWidget(parent, flags), mode(true)
{
    solvergui * solv = new solvergui;
    solveB = new QPushButton("Calculate", this);
    exitB = new QPushButton("Exit", this);
    changeModeB = new QPushButton("ChangeMode", this);

    oldBifX = new QString("0.01");    
    oldConvX = new QString("0.25");    
    oldConvR = new QString("1.6");
    oldBifR = new QString("0.01");
    oldConvIt = new QString("10");
    oldBifIt = new QString("1000");

    mainLayout = new QBoxLayout(QBoxLayout::LeftToRight, this);
    optLayout = new QBoxLayout(QBoxLayout::TopToBottom, this);

    dxLabel = new QLabel(this);
    itLabel = new QLabel(this);
    drLabel = new QLabel(this);
    modeLabel = new QLabel(this);

    dxLine = new QLineEdit(this);
    dxLine->setValidator(new QDoubleValidator(dxLine));
    itLine = new QLineEdit(this);
    itLine->setValidator(new QIntValidator(itLine));
    drLine = new QLineEdit(this);
    drLine->setValidator(new QDoubleValidator(drLine));

    optLayout->addWidget(modeLabel);
    optLayout->addWidget(changeModeB);
    optLayout->addWidget(dxLabel);
    optLayout->addWidget(dxLine);
    optLayout->addWidget(itLabel);
    optLayout->addWidget(itLine);
    optLayout->addWidget(drLabel);
    optLayout->addWidget(drLine);
    optLayout->addWidget(solveB);
    optLayout->addStretch(1);
    optLayout->addWidget(exitB);
    optLayout->setSizeConstraint(QLayout::SetFixedSize);

    drawer * draw = new drawer;

    mainLayout->addLayout(optLayout);
    mainLayout->addWidget(draw);
    
    QObject::connect(solv, SIGNAL(solved(std::pair<double, double> *, unsigned long long, bool)), draw, SLOT(data(std::pair<double, double> *, unsigned long long, bool)));
    QObject::connect(solveB, SIGNAL(clicked()), solv, SLOT(solve()));
    QObject::connect(exitB, SIGNAL(clicked()), this, SLOT(close()));
    QObject::connect(changeModeB, SIGNAL(clicked()), this, SLOT(change_mode()));    
    QObject::connect(dxLine, SIGNAL(textChanged(const QString &)), solv, SLOT(change_dx(const QString &)));    
    QObject::connect(itLine, SIGNAL(textChanged(const QString &)), solv, SLOT(change_itcount(const QString &)));    
    QObject::connect(drLine, SIGNAL(textChanged(const QString &)), solv, SLOT(change_dr(const QString &)));
    QObject::connect(this, SIGNAL(change_m(bool)), solv, SLOT(change_mode(bool)));
    QObject::connect(solv, SIGNAL(drawerSet(const PlotSettings &)), draw, SLOT(importSettings(const PlotSettings &)));

    setLayout(mainLayout);
    setBifMode();
}

void simpleiter::setBifMode()
{
    mode = true;
    modeLabel->setText("Mode: Bifurcation diagram");
    dxLabel->setText("X changing for one iteration");
    drLabel->setText("Parameter changing for one iteration");
    itLabel->setText("Iteration count");
    dxLine->setText(*oldBifX); 
    drLine->setText(*oldBifR);
    itLine->setText(*oldBifIt);
}

void simpleiter::setConvMode()
{
    mode = false;
    modeLabel->setText("Mode: Convergence trajectory");
    dxLabel->setText("First value of X");
    drLabel->setText("Parameter value");
    itLabel->setText("Iteration count");
    dxLine->setText(*oldConvX); 
    drLine->setText(*oldConvR);
    itLine->setText(*oldConvIt);
}

void simpleiter::change_mode()
{
    if (mode)
    {
        *(oldBifX) = dxLine->text();
        *(oldBifR) = drLine->text();
        *(oldBifIt) = itLine->text();
    }
    else
    {
        *(oldConvX) = dxLine->text();
        *(oldConvR) = drLine->text();
        *(oldConvIt) = itLine->text();
    }
    mode ? setConvMode() : setBifMode();
    emit change_m(mode);
}

simpleiter::~simpleiter()
{

}
